from nonebot import on_command, CommandSession, permission as perm
import nonebot,os
import datetime
@on_command('sign', aliases=('签到', '起床'), only_to_me=False)
async def sign(session: CommandSession):
    if (session.ctx['message_type'] == 'group'and session.ctx['group_id']==674496686):    
        hour=datetime.datetime.now().hour
        day=datetime.datetime.now().day
        month=datetime.datetime.now().month
        if (hour >= 4 and hour<8):
            if(session.ctx['anonymous']==None):
                user_name=str(session.ctx['sender']['card'])
                q=session.ctx['sender']['user_id']
                qq= str(session.ctx['sender']['user_id'])
                f=open("C:\\awesome-bot\\write_data.txt",encoding='utf-8')
                m=eval(f.read())
                f.close()
                f2=open("C:\\awesome-bot\\awesome\\plugins\\sign_log\\"+str(month)+"."+str(day)+".txt",'a+')
                size = os.path.getsize("C:\\awesome-bot\\awesome\\plugins\\sign_log\\"+str(month)+"."+str(day)+".txt")
                if (size==0):
                    p=[]
                else:
                    f2.seek(0)
                    p=eval(f2.read())
                    f2.close()
                if q in p:
                    await session.send('请勿重复签到', at_sender=True)
                else:
                    with open("C:\\awesome-bot\\awesome\\plugins\\sign_log\\"+str(month)+"."+str(day)+".txt",'a') as f:
                        f.write(str(qq)+',')
                    f.close()
                    with open("C:\\awesome-bot\\awesome\\plugins\\sign_log\\"+str(month)+"."+str(day)+"time"+".txt",'a') as f3:
                        f3.write("2018010203"+m[qq]+" 签到时间"+str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))+'\n')
                    f3.close()
                    await session.send('签到成功', at_sender=True)
            else:
                await session.send('匿名签到不算哦！', at_sender=True)
        else:
            await session.send('签到时间已过，下次早点。', at_sender=True)
    else:
        await session.send('请在不挂科群内签到', at_sender=True)

    

